Page({
    data: {
        oldPassword: '',
        newPassword: '',
        confirmNewPassword: ''
    },
    // 处理旧密码输入
    onOldPasswordInput(e) {
        this.setData({
            oldPassword: e.detail.value
        });
    },
    // 处理新密码输入
    onNewPasswordInput(e) {
        this.setData({
            newPassword: e.detail.value
        });
    },
    // 处理确认新密码输入
    onConfirmNewPasswordInput(e) {
        this.setData({
            confirmNewPassword: e.detail.value
        });
    },
    onClickLeft() {
        console.log("返回上一页");
        wx.navigateBack({
            delta: 1
        });
    },
    // 修改密码
    changePassword() {
        const { oldPassword, newPassword, confirmNewPassword } = this.data;
        if (!oldPassword || !newPassword || !confirmNewPassword) {
            wx.showToast({
                title: '请填写所有密码字段',
                icon: 'none'
            });
            return;
        }
        if (newPassword!== confirmNewPassword) {
            wx.showToast({
                title: '新密码和确认新密码不一致',
                icon: 'none'
            });
            return;
        }
        const app = getApp();
        const userId = app.globalData.userId;
        if (!userId) {
            wx.showToast({
                title: '用户未登录',
                icon: 'none'
            });
            return;
        }
        wx.request({
            url: 'http://localhost:8080/pubUser/changePasswordWithOld',
            method: 'PUT',
            header: {
              'Content-Type': 'application/x-www-form-urlencoded', // 根据服务器要求调整:ml-citation{ref="3,4" data="citationList"}
              'Authorization': 'Bearer token'    // 如需认证:ml-citation{ref="2" data="citationList"}
            },
            data: {
                id: userId,
                oldPassword,
                newPassword
            },
            success: (res) => {
                if (res.data.code === 1) {
                  console.log("ppp");
                    wx.showToast({
                        title: '密码修改成功',
                        icon: 'success'
                    });
                    wx.navigateBack();
                } else {
                    wx.showToast({
                        title: res.data.msg || '密码修改失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
            }
        });
    },
    // 取消修改
    cancelChange() {
        wx.navigateBack();
    }
  });